package org.TCC_NOVO.client.visual;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Window;
import java.util.Date;
import org.TCC_NOVO.client.GWTServiceAsync;

public class DisponibilizarOAForm extends Window{
    private final GWTServiceAsync controller;
    DisponibilizarOAPanel DispOAPanel;
    Panel panelPrin;
    FormPanel formPanel;
    WaitingBar wainting;
    FileUpload upload;
    TextArea textArea;
    TextArea textArqRaiz;
    String IDForm = "";
    String IDF_Usuario = "";
    String nmProjeto = "";
    String nmUnidade = "";

    public DisponibilizarOAForm(GWTServiceAsync controller, String ID_Form, String IDUsua, String nmProj, String nmUnidade,
                                DisponibilizarOAPanel DispOAPanel) {
        this.controller = controller;
        this.DispOAPanel = DispOAPanel;
        this.IDF_Usuario = IDUsua;
        this.IDForm = ID_Form;
        this.nmProjeto = nmProj;
        this.nmUnidade = nmUnidade;
        this.init();
    }

    private void init(){
        panelPrin = getPanelCentral();
        this.add(panelPrin);
        this.setTitle("Upload do Material");
    }

    private Panel getPanelCentral(){
        Panel panelRes = new Panel();
        panelRes.setBorder(false);
        panelRes.setPaddings(1);

        //wainting bar para fazer o progresso...
        wainting = new WaitingBar("Upload");

        final FormPanel form = new FormPanel();
        form.setAction(/*GWT.getModuleBaseURL() + */"ServletUpload");
        form.setEncoding(FormPanel.ENCODING_MULTIPART);
        form.setMethod(FormPanel.METHOD_POST);

        VerticalPanel panel = new VerticalPanel();
        form.setWidget(panel);

        textArea = new TextArea();
        textArea.setTitle("Observação");
        textArea.setName("observacao");
        textArea.setSize("280", "110");
        panel.add(textArea);

        textArqRaiz = new TextArea();
        textArqRaiz.setTitle("Arquivo Raiz");
        textArqRaiz.setName("arquivo_raiz");
        textArqRaiz.setSize("280", "20");
        panel.add(textArqRaiz);

        //Criação do FileUpload widget.
        upload = new FileUpload();
        upload.setName("Unidades"+","+IDForm+","+nmProjeto+","+nmUnidade);

        panel.add(upload);
        Button button2 = new Button("Upload", new ClickListener() {
            @Override
            public void onClick(Widget sender) {
                try {
                form.submit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        panel.add(button2);

        final AsyncCallback callbackUnidadesOco = new AsyncCallback() {
            @Override
            public void onSuccess(Object result) {
                wainting.hide();
                hide();
            }
            @Override
            public void onFailure(Throwable caught) {
               MessageBox.alert("Erro ", caught.getMessage()); }
        };

        final AsyncCallback callbackUnidades = new AsyncCallback() {
            @Override
            public void onSuccess(Object result) {
                //wainting.hide();
                //hide();
            }
            @Override
            public void onFailure(Throwable caught) {
               MessageBox.alert("Erro ", caught.getMessage()); }
        };

        //Adiciona o evento no form.
        form.addFormHandler(new FormHandler() {
            @Override
            public void onSubmitComplete(FormSubmitCompleteEvent event) {
                System.out.println(event.getResults());
                //MessageBox.alert("Funcou");

                //altera status da Unidade
                String sSql = "update UNIDADE set status = 'Pronta para Revisão', Arquivo_Raiz = '"+textArqRaiz.getText()+"' where ID = "+IDForm;
                controller.EditarRegistro(sSql, callbackUnidades);

                //Incluir ocorrencia de tarefa
                                   DateTimeFormat sdf = DateTimeFormat.getFormat("yyyy-MM-dd");

               String DataOco = sdf.format(new Date());
                //MessageBox.alert(data.toString());
                sSql = "insert into OCORRENICA_UNIDADE (IDF_Unidade, IDF_Usuario, Descricao, Data, Observacao) values "+
                         "("+IDForm+", "+IDF_Usuario+",'Status alterado para Pronta para Revisão'" +", '"+DataOco+ "', '"+textArea.getText()+"' )";
                controller.IncluirRegistro(sSql, callbackUnidadesOco);

                //atualizar grid
                DispOAPanel.CarrgarGrid();
            }

            public void onSubmit(FormSubmitEvent event) {
                wainting.show();
                wainting.Executar();
            }
        });

        panelRes.add(form);
        return panelRes;
    }

}
